﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SecsEquip;

namespace EquipSecs300
{
    public partial class PJobView : CMyFormEx
    {
        public GEM_PJob GEMPJob { get; set; }
        private int _PJobCountPre;

        OnPJobCreateHandler m_pOnPJobCreateView;

        public PJobView()
        {
            m_pViewName = "PJob";
            InitializeComponent();
            //初始化控件
            InitCtrl();
        }

        // 更新数据
        public override void UpdateData()
        {
            GEMPJob = secsEquipHelper.PJob;
            GEMPJob.PJobStageChange += OnListViewUpdata;
        }

        //初始化控件
        void InitCtrl()
        {
            listViewJob.Columns.Add("PJobID", 200, HorizontalAlignment.Left);
            listViewJob.Columns.Add("CreateTime", 150, HorizontalAlignment.Left);
            listViewJob.Columns.Add("UpdateTime", 150, HorizontalAlignment.Left);
            listViewJob.Columns.Add("State", 100, HorizontalAlignment.Left);
            listViewJob.Columns.Add("Start", 100, HorizontalAlignment.Left);
            listViewJob.Columns.Add("RecipeID", 200, HorizontalAlignment.Left);
            listViewJob.Columns.Add("MaterialList", 300, HorizontalAlignment.Left);

            OnPasswordLogin(GlobalVar.IsLogIn);
        }

        // 获取选中的PJobID
        string GetSelectPJobID()
        {
            string pPJobID = null;
            if (listViewJob.SelectedItems.Count > 0)
            {
                pPJobID = listViewJob.SelectedItems[0].SubItems[0].Text;
            }
            return pPJobID;
        }

        // 开始
        private void BtnJobStart_Click(object sender, EventArgs e)
        {
            GEMPJob.UpdateSelectJobState(GetSelectPJobID(), ProcessJobState.PROCESSING);
        }

        private void BtnJobPause_Click(object sender, EventArgs e)
        {
            GEMPJob.UpdateSelectJobState(GetSelectPJobID(), ProcessJobState.PAUSED);
        }

        private void BtnJobResume_Click(object sender, EventArgs e)
        {
            GEMPJob.UpdateSelectJobState(GetSelectPJobID(), ProcessJobState.PROCESSING);
        }

        private void BtnJobStop_Click(object sender, EventArgs e)
        {
            GEMPJob.UpdateSelectJobState(GetSelectPJobID(), ProcessJobState.STOPPED);
        }

        private void BtnJobAbort_Click(object sender, EventArgs e)
        {
            GEMPJob.UpdateSelectJobState(GetSelectPJobID(), ProcessJobState.ABORTED);
        }

        private void btnJobComplete_Click(object sender, EventArgs e)
        {
            GEMPJob.UpdateSelectJobState(GetSelectPJobID(), ProcessJobState.COMPLETE);
        }

        // 删除
        private void btnJobDelete_Click(object sender, EventArgs e)
        {
            string pPJobID = GetSelectPJobID();
            GEMPJob.JobDelete(pPJobID);

        }
        private void btnJobDeleteAll_Click(object sender, EventArgs e)
        {
            GEMPJob.JobDeleteAll();
        }

        private void btnJobDeleteAllForce_Click(object sender, EventArgs e)
        {
            GEMPJob.JobDeleteAllForce();
        }

        private void btnRefesh_Click(object sender, EventArgs e)
        {
            ListViewUpdata();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (GEMPJob.PJobListAll.Count != _PJobCountPre)
            {
                _PJobCountPre = GEMPJob.PJobListAll.Count;
                ListViewUpdata();
            }
        }

        private void OnListViewUpdata()
        {
            BeginInvoke(new Action(() => { ListViewUpdata(); }));
        }
        private void ListViewUpdata()
        {
            listViewJob.Items.Clear();

            for (int j = 0; j < GEMPJob.PJobListAll.Count; j++)
            {
                ProcessJob pJob = GEMPJob.PJobListAll[j];
                // 材料清单
                List<string> listSlot = new List<string>();
                for (int i = 0; i != pJob.listSlot.Count; i++)
                {
                    listSlot.Add(pJob.listSlot[i].ToString());
                }
                string pSlot = CListOperate.listJoin(listSlot);
                string pCarrierInfo = CListOperate.listJoin(pJob.pCarrierID, pSlot);

                // 添加数据到窗口
                listViewJob.BeginUpdate();

                ListViewItem lvi = new ListViewItem();
                lvi.Text = pJob.pPJobID;
                lvi.SubItems.Add(pJob.pPRTimeCreated);
                lvi.SubItems.Add(pJob.pPRTimeUpdated);
                lvi.SubItems.Add(pJob.nPRJobState.ToString());
                lvi.SubItems.Add(pJob.nPRProcessStart.ToString());
                lvi.SubItems.Add(pJob.pRecipe);
                lvi.SubItems.Add(pCarrierInfo);
                listViewJob.Items.Add(lvi);

                listViewJob.EndUpdate();
            }
        }

        public override void OnPasswordLogin(int permission)
        {
            if (permission == 1)
            {
                panelPermission.Show();
            }
            else
            {
                panelPermission.Hide();
            }

        }


    }
}
